package com.itcast.catchfiveeight;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/2/22 0022.
 */
public class MergeFiles {


        public static List<String> list = new ArrayList<String>();
        public static void main(String[] agrs)throws Exception{
            //首先递归读取文件
            //获取到所有文件名称以及文件的路径
            String filePath = Contents.tmpPath;
            getFiles(filePath);
        }

       /*
       * 得到路径下所有文件
       */
        static void getFiles( String filePath )throws Exception
        {
            //
            File root = new File( filePath );
            File[] files = root.listFiles();
//            String ffp = Contents.finalPath;
//            String fileName = Contents.fileName;
            String ffp = Contents.finalPath;
            String fileName = Contents.fileName;
            File fileses = new File(ffp,fileName);
            int j = 0;
            CatchLinks.writeTitle(fileses);
            for ( File file : files ) {
                    //读取文件
                    // 读取文件内容 (输入流)
                    List<String> list = FileUtils.readLines(file,"GB2312");
                    for(int i=1;i<list.size();i++){
                        FileUtils.write(fileses,list.get(i), "GBK", true);
                        FileUtils.write(fileses, "\r\n", "GBK", true);
                    }
                j++;
                System.out.println(j);
            }
        }
}
